package com.yunji.base.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.base.dao.ISuggestDao;
import com.yunji.base.domain.SuggestDO;
import com.yunji.base.dto.SuggestReadDTO;
import com.yunji.base.mapper.ISuggestMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.model.params.SuggestParam;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.message.dao.impl
 * @Title: SuggestDaoImpl
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-10-30 17:43:29      V1.0        HuaAo       新建类
 */
@Repository
public class SuggestDaoImpl extends BaseDaoImpl<ISuggestMapper, SuggestDO> implements ISuggestDao {

    @Override
    public List<SuggestDO> searchPageList(SuggestParam param) {
        return pageList(param, new LambdaQueryWrapper<SuggestDO>()
                .like(BeanUtils.isNotNull(param.getUserName()), SuggestDO::getUserName, param.getUserName())
                .like(BeanUtils.isNotNull(param.getPhoneNumber()), SuggestDO::getPhoneNumber, param.getPhoneNumber())
                .orderByDesc(SuggestDO::getCreateTime));
    }

    @Override
    public List<String> searchTableField(SuggestParam param) {
        LambdaQueryWrapper<SuggestDO> wrapper = new QueryWrapper<SuggestDO>()
                .select(param.getSearchField()).lambda()
                .like(BeanUtils.isNotNull(param.getUserName()), SuggestDO::getUserName, param.getUserName())
                .like(BeanUtils.isNotNull(param.getPhoneNumber()), SuggestDO::getPhoneNumber, param.getPhoneNumber())
                .last("limit 5");
        return listObjs(wrapper, Object::toString).stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<SuggestDO> search(SuggestParam param) {
        return list(new LambdaQueryWrapper<SuggestDO>()
                .like(BeanUtils.isNotNull(param.getUserName()), SuggestDO::getUserName, param.getUserName())
                .like(BeanUtils.isNotNull(param.getPhoneNumber()), SuggestDO::getPhoneNumber, param.getUserName()));
    }

    @Override
    public boolean updateReadStatus(SuggestReadDTO dto) {
        return update(new LambdaUpdateWrapper<SuggestDO>()
                .set(SuggestDO::getReadStatus, dto.getReadStatus())
                .eq(SuggestDO::getSuggestId, dto.getSuggestId()));
    }

}